package ui;

import io.UserAccess;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Iterator;
import java.util.List;

import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ScrollPaneConstants;

import users.Role;
import users.User;


/**
 * A panel that displays the list of users in the database.
 * 
 * @author Aaron Chen
 * @version 3/20/2014
 */
@SuppressWarnings("serial")
public class UserListPanel extends AbstractMainComponent {

  /** The index value of the user full name column. */
  private static final int COLUMN_NAME_INDEX = 0;
  /** The index value of the user email column. */
  private static final int COLUMN_EMAIL_INDEX = 1;
  /** The index value of the user roles column. */
  private static final int COLUMN_ROLES_INDEX = 2;
  /** The index value of the user select button column. */
  private static final int COLUMN_BUTTON_INDEX = 3;
  
  /** The header labels. */
  private static final String[] HEADERS = {
    "Name",
    "Email",
    "Roles",
    "" //select button
  };

  /** The columns that will be marked as editable and thus have buttons. */
  private static final boolean[] BOOLS = {false, false, false, true};
  
  /** The panel that is waiting for input. */
  private Recipient my_recipient;
  
  
  /**
   * Create the panel that holds user listings.
   * @param the_parent The main application window container.
   * @param the_recip  The recipient waiting for input from this panel.
   */
  public UserListPanel(final DisplayPanel the_parent, final Recipient the_recip) {
    super(the_parent);
    setLayout(new BorderLayout());
    setBackground(Standards.COLOR_BACKGROUND);
    my_recipient = the_recip;
    final JTable user_table = createUserTable();
    final JScrollPane scroll_pane = createScrollPane(user_table);
    add(user_table.getTableHeader(), BorderLayout.NORTH);
    add(scroll_pane, BorderLayout.CENTER);
  }
  
  /**
   * Creates a scroll pane specially suited for the table.
   * @param the_component The table to be made scrollable.
   * @return The scroll pane.
   */
  private JScrollPane createScrollPane(final Component the_component) {
    final JScrollPane scroll_pane = new JScrollPane(the_component);
    scroll_pane.getViewport().setBackground(Standards.COLOR_BACKGROUND);
    scroll_pane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
    scroll_pane.getVerticalScrollBar().setUnitIncrement(Standards.SCROLL_BAR_INCREMENT);
    scroll_pane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
    return scroll_pane;
  }
  
  /**
   * Creates a new JTable with brief conference information in each row and a
   * button for viewing each conference at the end.
   * @return A JTable with one conference per row.
   */
  private JTable createUserTable() {
    final UserAccess database = MainPage.getDatabase().accessUser();
    final List<User> list = database.getAllUser();
    final Object[][] data = new Object[list.size()][HEADERS.length];
    final ActionListener[][] als = new ActionListener[list.size()][HEADERS.length];
    final Iterator<User> itr = list.iterator();
    for (int i = 0; i < data.length; i++) {
      final User u = itr.next();
      data[i][COLUMN_NAME_INDEX] = u.getFullName();
      data[i][COLUMN_EMAIL_INDEX] = u.getEmail();
      final List<Role> roles = u.getRoles();
      String role_str = null;
      if (!roles.isEmpty()) {
        final Iterator<Role> r_itr = roles.iterator();
        final StringBuilder sb = new StringBuilder(r_itr.next().getRoleName());
        while (r_itr.hasNext()) {
          sb.append(", ");
          sb.append(r_itr.next().getRoleName());
        }
        role_str = sb.toString();
      }
      data[i][COLUMN_ROLES_INDEX] = role_str;
      data[i][COLUMN_BUTTON_INDEX] = "Choose User";
      als[i][COLUMN_BUTTON_INDEX] = new ActionListener() {
        @Override
        public void actionPerformed(final ActionEvent the_e) {
          my_recipient.receive(u);
          final DisplayPanel dsp = getDisplayStack();
          dsp.removeFromStack();
        }
      };
    }
    return new CustomTable(data, HEADERS, BOOLS, als);
  }
  
  /**
   * {@inheritDoc}
   */
  @Override
  public String getPanelLabel() {
    return "Select User";
  }

}
